import * as THREE from 'three';
import { Tshare } from '../utils/Tshare';

let container = document.querySelector("#app"),
T = new Tshare(),
pointLight = null,
pointLight1 = null,
pointLight2 = null

init();
animate();

function init() {
    T.initEnv(container).initOrbitControls();
    let sphere =T.createSphereMesh();
    sphere.castShadow = true;
    T.sceneGraph.add(sphere);
    T.scene.fog = new THREE.Fog(0x262f45,2,10);
    T.renderer.shadowMap.enabled = true;
    T.renderer.physicallyCorrenterLight = true
    T.camera.position.y = 1.5;
    T.camera.lookAt(T.scene.position)
    addGround();
    addPointLight()
}

function animate() {
    requestAnimationFrame(animate);
    T.renderer.render(T.scene,T.camera);

    pointLight.position.x = Math.sin(Date.now() * 0.001) * 1;
    pointLight.position.y = Math.sin(Date.now() * 0.002) * 1;
    pointLight.position.z = Math.sin(Date.now() * 0.0015) * 1;

    pointLight1.position.x = Math.cos(Date.now() * 0.001) * 1;
    pointLight1.position.y = Math.sin(Date.now() * 0.002) * 1;
    pointLight1.position.z = Math.sin(Date.now() * 0.0015) * 1;

    pointLight2.position.x = Math.cos(Date.now() * 0.001) * 1;
    pointLight2.position.y = Math.sin(Date.now() * 0.002) * 1;
    pointLight2.position.z = Math.cos(Date.now() * 0.0015) * 1;
}
function addGround() {
    let ground = new THREE.Mesh(
        new THREE.PlaneGeometry(100,100),
        new THREE.MeshStandardMaterial({color: 0x262f45})
    )
    ground.rotateX(-Math.PI/2);
    ground.position.y -= 0.5;
    ground.receiveShadow = true;
    T.sceneGraph.add(ground);
}
function addPointLight() {
    let sphereGeo = new THREE.SphereGeometry(0.05,20,20);
    T.scene.add(new THREE.AmbientLight(0x262f45,1))
    pointLight = getPointsLight(0xeb8dd0,sphereGeo);
    pointLight1 = getPointsLight(0x71ebe3,sphereGeo);
    pointLight2 = getPointsLight(0x46eb34,sphereGeo);
    T.scene.add(pointLight,pointLight1,pointLight2);
}
function getPointsLight(color,sphereGeo) {
    let pointLight = new THREE.PointLight(color,1);
    pointLight.castShadow = true;
    let lightSphere = new THREE.Mesh(
        sphereGeo,
        new THREE.MeshBasicMaterial({color})
    )
    pointLight.add(lightSphere);
    return pointLight;
}